package org.hxyjs.dao.impl;

import org.hxyjs.dao.AdvertisersDao;
import org.hxyjs.entity.Advertisers;
import org.hxyjs.entity.Advertisers;
import org.hxyjs.mapper.AdvertisersMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("advertisersDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class AdvertisersDaoImpl implements AdvertisersDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private AdvertisersMapper advertisersMapper;
    public List<Advertisers>  getAllAdvertiserss() throws Exception{
        return advertisersMapper.selectByExample(null);
    }

    @Override
    public Integer deleteAdvertisersByAdno(String adno)  throws Exception{
        return advertisersMapper.deleteByPrimaryKey(adno);
    }

    @Override
    public Integer updateAdvertisersByAdno(Advertisers advertisers)  throws Exception{
        return advertisersMapper.updateByPrimaryKey(advertisers);
    }

    @Override
    public Integer addAdvertisers(Advertisers advertisers)  throws Exception{
        advertisers.setAdno(initId());
        return advertisersMapper.insertSelective(advertisers);
    }
    @Override
    public List<Advertisers> getAdvertisersByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)advertisersMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return advertisersMapper.getAdvertisersByPage(map);
    }

    public String initId(){
        String maxAdno = advertisersMapper.getMaxAdno();
        System.out.println(GenNumTools.initId("",3,maxAdno));
        return GenNumTools.initId("",3,maxAdno);
    }


    @Override
    public Advertisers getAccountByAccountName(String adname) {
        return advertisersMapper.getAccountByAccountName(adname);
    }


}
